var mongoose=require('mongoose');
var User=mongoose.model('User',{
    name:String,
    number:Number,
    winner:Boolean,
    array:Array
});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');


User.update({name:'A'},{number:1101,winner:true,array:[1,2,3]},{upsert:true},(e,r)=>{
    if(e)throw e;
    console.log('Update:\n',r);

    User.update({name:'B'},{number:1201,winner:false,array:[1,2]},{upsert:true},(e,r)=>{
        if(e)throw e;
        console.log('Update:\n',r);


        User.update({name:'C'},{number:1301,winner:true,array:[1]},{upsert:true},(e,r)=>{
            if(e)throw e;
            console.log('Update:\n',r);

            User.update({name:'D'},{number:1401,winner:false,array:[1,4]},{upsert:true},(e,r)=>{
                if(e)throw e;
                console.log('Update:\n',r);

                User.update({name:'E'},{number:1501,winner:false,array:[3,2,1]},{upsert:true},(e,r)=>{
                    if(e)throw e;
                    console.log('Update:\n',r);

                    User.find((e,r)=>{
                        if(e)throw e;
                        console.log('\nFind All:\n',r);

                        User.find({
                            array:{
                                $all:[1,2,3]
                            }
                        },{number:true,array:true},(e,r)=>{
                            if(e)throw e;
                            console.log('\n====Find 1:\n',r);

                            User.find({
                                array:[1,2,3]
                            },{number:true,array:true},(e,r)=>{
                                if(e)throw e;
                                console.log('\n====Find 2:\n',r);


                                User.find({
                                    array:{
                                        $in:[1,2,3]
                                    }
                                },{number:true,array:true},(e,r)=>{
                                    if(e)throw e;
                                    console.log('\n====Find 3:\n',r);


                                    User.find({
                                        array:1
                                    },{number:true,array:true},(e,r)=>{
                                        if(e)throw e;
                                        console.log('\n====Find 4:\n',r);
                                        process.exit(1);
                                    });

                                });

                            });

                        });

                    });


                });


            });

        });

    });

});


/*
* * field:{$all:[val1,val2,...]}
* 数组字段的值相等于：val1,val2,...(【不需要】索引值对应)
*
*
* * field:[val1,val2...]
* 匹配数组字段的值完全相等于：val1,val2,...(【需要】索引值对应)
*
* * field:{$in:[val1,val2,...]}
* 匹配数组字段的值包含：val1或val2...
*
* * field:val
* 匹配数组字段的值包含：val (相当于 $in:[val])
* */
